package cn.iocoder.yudao.module.book.service.members;

import cn.iocoder.yudao.module.book.controller.app.classes.vo.AppClassMembersSaveReqVO;
import cn.iocoder.yudao.module.book.dal.dataobject.classes.ClassesDO;
import cn.iocoder.yudao.module.book.dal.dataobject.classesAndMember.ClassesAndMember;
import cn.iocoder.yudao.module.book.dal.mysql.classesAndMember.ClassesAndMemberMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.book.controller.admin.members.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.members.ClassMembersDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.book.dal.mysql.members.ClassMembersMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.*;

/**
 * 班级成员 Service 实现类
 *
 * @author xi
 */
@Service
@Validated
public class ClassMembersServiceImpl implements ClassMembersService {

    @Resource
    private ClassMembersMapper classMembersMapper;

    @Resource
    private ClassesAndMemberMapper classesAndMemberMapper;

    @Override
    public Integer createClassMembers(ClassMembersSaveReqVO createReqVO) {
        // 插入
        ClassMembersDO classMembers = BeanUtils.toBean(createReqVO, ClassMembersDO.class);
        classMembersMapper.insert(classMembers);
        // 返回
        return classMembers.getMembersId();
    }

    @Override
    public void updateClassMembers(ClassMembersSaveReqVO updateReqVO) {
        // 校验存在
        validateClassMembersExists(updateReqVO.getMembersId());
        // 更新
        ClassMembersDO updateObj = BeanUtils.toBean(updateReqVO, ClassMembersDO.class);
        classMembersMapper.updateById(updateObj);
    }

    @Override
    public void deleteClassMembers(Integer id) {
        // 校验存在
        validateClassMembersExists(id);
        // 删除
        classMembersMapper.deleteById(id);
    }

    private void validateClassMembersExists(Integer id) {
        if (classMembersMapper.selectById(id) == null) {
            throw exception(CLASS_MEMBERS_NOT_EXISTS);
        }
    }

    @Override
    public ClassMembersDO getClassMembers(Integer id) {
        return classMembersMapper.selectById(id);
    }

    @Override
    public PageResult<ClassMembersDO> getClassMembersPage(ClassMembersPageReqVO pageReqVO) {
        return classMembersMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional
    public Integer InsertClassMembers(AppClassMembersSaveReqVO member) {
        ClassMembersDO classes = BeanUtils.toBean(member, ClassMembersDO.class);
        classes.setCreateTime(LocalDateTime.now());
        classes.setUpdateTime(LocalDateTime.now());
        classMembersMapper.insert(classes);
        Integer id = classes.getMembersId();
        System.out.println( classes);

        ClassesAndMember classesAndMember = new ClassesAndMember();
        classesAndMember.setClassesId(classes.getClassesId());
        classesAndMember.setMembersId(id);
        System.out.println( classesAndMember);
        classesAndMemberMapper.insertData(classes.getClassesId(), id);
        return member.getMembersId();
    }

    @Override
    public List<ClassMembersDO> getClassList(Long userId) {
        return classMembersMapper.selectClassesList(userId);
    }

}